﻿using EONFULLSIN2.Core;
using EONFULLSIN2.Core.Extensions;
using EONFULLSIN2.Models.Attributes;
using EONFULLSIN2.Models.Dto.Device;
using EONFULLSIN2.Models.Enum;
using EONFULLSIN2.Models.Enum.IOPointEnum;
using EONFULLSIN2.Services.DB;
using EONFULLSIN2.Services.Device;
using EONFULLSIN2.Services.Device.DIOModelA;
using EONFULLSIN2.Services.Device.DIOModelB;
using EONFULLSIN2.Services.Device.JuYing;
using EONFULLSIN2.Services.Device.ZhuoLan;
using EONFULLSIN2.Services.Device.ZMoiton;
using EONFULLSIN2.Services.Message;
using EONFULLSIN2.ViewModelBase;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EONFULLSIN2.ViewModel {
    public class DeviceOperationViewModel : NavigationViewModel {
        private readonly IJuYingModbusService _juYingModbusService;
        private readonly IZhuoLanModbusService _zhuoLanModbusService;
        private readonly IZMoitonService _zMoitonService;

        public DeviceOperationViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IMessageService messageBox,
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate,
            IJuYingModbusService juYingModbusService,
            IZhuoLanModbusService zhuoLanModbusService,
            IZMoitonService zMoitonService
            )
            : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate) {

            this._juYingModbusService = juYingModbusService;
            this._zhuoLanModbusService = zhuoLanModbusService;
            this._zMoitonService = zMoitonService;

            JYIPItems.Add(new TcpModbusItemsDto { ModbusBaseServices = this._juYingModbusService });
            ZLANItems.Add(new TcpModbusItemsDto { ModbusBaseServices = this._zhuoLanModbusService });

            //获取状态
            PollingGetDOChannelStatus();
        }

        #region Props

        private ObservableCollection<TcpModbusItemsDto> _jYIPItems = new ObservableCollection<TcpModbusItemsDto>();

        public ObservableCollection<TcpModbusItemsDto> JYIPItems {
            get { return _jYIPItems; }
            set { _jYIPItems = value; base.RaisePropertyChanged(); }
        }

        private ObservableCollection<TcpModbusItemsDto> _zLANItems = new ObservableCollection<TcpModbusItemsDto>();

        public ObservableCollection<TcpModbusItemsDto> ZLANItems {
            get { return _zLANItems; }
            set { _zLANItems = value; base.RaisePropertyChanged(); }
        }

        private ObservableCollection<SensorItemDto> _sensorItemDtos = new ObservableCollection<SensorItemDto>();

        public ObservableCollection<SensorItemDto> SensorItemDtos {
            get { return _sensorItemDtos; }
            set { _sensorItemDtos = value; base.RaisePropertyChanged(); }
        }
        #endregion


        #region Commands
        public DelegateCommand<TcpModbusItemsDto> ZlanGetDataCmd => new DelegateCommand<TcpModbusItemsDto>((obj) => {
            Debug.WriteLine(JsonConvert.SerializeObject(obj));
        });

        public DelegateCommand<TcpModbusItemsDto> ZlanSendDataCmd => new DelegateCommand<TcpModbusItemsDto>((obj) => {

            Debug.WriteLine(JsonConvert.SerializeObject(obj));
        });

        public DelegateCommand<TcpModbusItemsDto> JYGetDataCmd => new DelegateCommand<TcpModbusItemsDto>((obj) => {
            Debug.WriteLine(JsonConvert.SerializeObject(obj));

        });

        public DelegateCommand<TcpModbusItemsDto> JYSendDataCmd => new DelegateCommand<TcpModbusItemsDto>((obj) => {
            Debug.WriteLine(JsonConvert.SerializeObject(obj));

        });

        public DelegateCommand<SensorItemDto> IOSendDataCmd => new DelegateCommand<SensorItemDto>(async (obj) => {

            _ = _zMoitonService.SetOutPutStatusByIndexAsync((eIOPointEnum)obj.DataSourceAddress, !(bool)obj.Value);

        });
        #endregion




        #region Methods
        private void PollingGetDOChannelStatus() {

            _ = Task.Factory.StartNew(async () => {
                try {
                    while (true) {
                        var ioStatus = await _zMoitonService.GetSensorOutPutStatusAsync();
                        _ = App.Current.Dispatcher.BeginInvoke(() => {

                            if (SensorItemDtos.Count == 0) {
                                SensorItemDtos.AddRange(ioStatus);
                            } else {
                                if (ioStatus.Count > 0)
                                {
                                    for (int i = 0; i < SensorItemDtos.Count; i++)
                                    {
                                        SensorItemDtos[i].Value = ioStatus?[i].Value;
                                    }
                                }
                            }
                        });
                        await Task.Delay(500);//每间隔1s获取一次
                    }
                } catch (Exception ex) {

                    LogManager.Error(ex);
                    Console.WriteLine(ex.Message);
                    //throw;
                }
            });
        }
        #endregion

    }

    //[AddINotifyPropertyChangedInterface]
    public class TcpModbusItemsDto {
        public IModbusBaseServices ModbusBaseServices { get; set; }

        /// <summary>
        /// 发送数据地址
        /// </summary>
        public string SendDataAddress { get; set; }

        /// <summary>
        /// 获取数据地址
        /// </summary>
        public string GetDataAddress { get; set; }
    }

    //[AddINotifyPropertyChangedInterface]
    public class AdamComItemsDto {
        public IAdamComBaseServices AdamComBaseServices { get; set; }

        public ObservableCollection<SensorItemDto> SensorItemDtos { get; set; }
    }

}
